package com.cpic.rxjava;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

/**
 * https://www.jianshu.com/p/263bc8f3f960
 * https://www.jianshu.com/p/ba1835f65f89?from=timeline
 *
 * https://www.jianshu.com/p/3dd582bb10cc
 * https://blog.csdn.net/jiang19921002/article/details/85011695
 */
public class MainActivity extends AppCompatActivity {
    final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test3();
    }


    void test1() {
        Obsersable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subcribe(Emitter<String> emmiter) {
                emmiter.onNext("xxxx");

            }
        }).subcribeObserver(new Observer<String>() {
            @Override
            public void onNext(String s) {
                Log.e(TAG, s);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onSubcribe() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    void test2() {
        Obsersable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subcribe(Emitter<String> emmiter) {
                emmiter.onNext("xxxx");
                emmiter.onComplete();
            }
        }).map(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return 321;
            }
        }).map(new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) {
                return "ccc = " + integer.toString();
            }
        })
                .subcribeObserver(new MyObserver());
    }

    void test3(){
        Log.e(TAG, "start    "+Thread.currentThread().getId());
        Obsersable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subcribe(Emitter<String> emmiter) {
                emmiter.onNext("xxxx");
                Log.e(TAG, "ObservableOnSubscribe    "+Thread.currentThread().getId());
            }
        }).subScribeOn(-1).obverserOn(1)

                .subcribeObserver(new Observer<String>() {
            @Override
            public void onNext(String s) {
                Log.e(TAG, s);

                Log.e(TAG, "onNext    "+Thread.currentThread().getId());

            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onSubcribe() {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    class MyObserver implements Observer<String >{

        public MyObserver() {
            Log.e("MyObserver","id = "+this.toString());

        }

        @Override
        public void onNext(String s) {
            Log.e(TAG, s);
            Log.e("Observer","onNext = "+this.toString());

        }

        @Override
        public void onError(Throwable throwable) {

        }

        @Override
        public void onSubcribe() {
            Log.e(TAG, "onSubcribe");
            Log.e("Observer","onSubcribe = "+this.toString());
        }

        @Override
        public void onComplete() {
            Log.e(TAG, "onComplete");
            Log.e("Observer","onComplete = "+this.toString());

        }
    }
}